$(document).ready(function() {
	selida.setup({accountPage: true});
	$('<iframe>').attr({name: 'account'}).appendTo($('#basiKato'));

	Account.titlosDOM = $('.formaTitlos');
	Account.formaDOM = $('.formaSoma');
	Account.loginDOM = $('input[name="login"]');
	Account.onomaDOM = $('input[name="onoma"]');
	Account.emailDOM = $('input[name="email"]');
	Account.kodikosDOM = $('input[name="kodikos"]');
	Account.kodikos1DOM = $('input[name="kodikos1"]');
	Account.kodikos2DOM = $('input[name="kodikos2"]');

	if (client.isPektis()) {
		Account.titlosDOM.text('Ενημέρωση στοιχείων λογαριασμού');
		Account.loginDOM.prop({disabled: true}).val(client.session.pektis);
		Account.onomaDOM.focus();
		$('#kodikos1,#kodikos2').css({display: 'none'});
		$('#kodikos,#alagiKodikos').css({display: 'table-row'});
		Account.pektisGet();
	}
	else {
		Account.titlosDOM.text('Δημιουργία λογαριασμού');
		Account.loginDOM.focus().on('keyup', function(e) {
			Account.loginCheckit($(this));
		});
		$('#kodikos1,#kodikos2').css({display: 'table-row'});
		$('#kodikos,#alagiKodikos').css({display: 'none'});
	}
});

Account = {};

Account.pektisGet = function() {
	client.fyi.pano('Ενημέρωση στοιχείων λογαριασμού. Παρακαλώ περιμένετε…');
	client.node('pektisGet', function(rsp) {
		if (rsp.sfalma) return;
		try {
			eval('var pektis = ' + rsp.responseText + ';');
		} catch(e) {
			return client.fyi.epano(rsp.responseText);
		}

		client.fyi.pano();
		Account.onomaDOM.val(pektis.onoma);
		Account.emailDOM.val(pektis.email);
	});
}

Account.alagiKodikos = function(alagi) {
	if (alagi === false) {
		$('#kodikos1,#kodikos2').css({display: 'none'});
		$('#alagiKodikos').css({display: 'table-row'});
	}
	else {
		$('#kodikos1,#kodikos2').css({display: 'table-row'});
		$('#alagiKodikos').css({display: 'none'});
		Account.kodikos1DOM.focus();
	}

	return false;
}

Account.submit = function(form) {
	if (Account.proxori === false) return false;
	if (Account.proxori === true) return true;
	Account.proxori = false;

	// Έλεγχοι

	if (client.isPektis()) Account.enimerosi();
	else Account.egrafi(form);

	return false;
}

Account.enimerosi = function() {
	var params, kodikos1 = Account.kodikos1DOM.val();

	if (Account.kodikos2DOM.val() != kodikos1) {
		delete Account.proxori;
		client.fyi.spano('Διαφορετικοί κωδικοί');
		Account.kodikos1DOM.focus();
		return;
	}

	params = 'onoma=' + Account.onomaDOM.val().uri() +
		'&email=' + Account.emailDOM.val().uri() +
		'&kodikos=' + Account.kodikosDOM.val().uri();

	if (kodikos1) params += '&kodikos1=' + Account.kodikos1DOM.val().uri();
	Account.working();
	client.fyi.pano('Ενημέρωση στοιχείων λογαριασμού. Παρακαλώ περιμένετε…');
	client.node('pektisEnimerosi', params,
	function(xhr) {
		delete Account.proxori;
		if (Account.apotixia(xhr)) return;
		Account.alagiKodikos(false);
		Account.working('cpanel/apodoxi.png').delay(1000).fadeOut();
	});
}

Account.egrafi = function(form) {
	Account.working();
	client.fyi.pano('Δημιουργία λογαριασμού. Παρακαλώ περιμένετε…');

	client.session.pektis = Account.loginDOM.val();
	client.session.klidi = globals.randomString(10);

	client.node('pektisEgrafi',
		'onoma=' + Account.onomaDOM.val().uri(),
		'email=' + Account.emailDOM.val().uri(),
		'kodikos=' + Account.kodikos1DOM.val().uri(),
	function(xhr) {
		if (Account.apotixia(xhr)) return;
		Account.working('working/prasino.gif');
		client.ajax('login/checkin', 'klidi=' + client.session.klidi.uri(),
		function(xhr) {
			if (Account.apotixia(xhr)) return;
			Account.proxori = true;
			$(form).submit();
		});
	});
}

Account.apotixia = function(xhr) {
	if (xhr.sfalma) {
		Account.working(false);
		delete Account.proxori;
		return true;
	}

	client.fyi.spano(xhr.responseText);
	if (xhr.responseText) {
		Account.working(false);
		delete Account.proxori;
		return true;
	}

	return false;
}

Account.loginCheckit = function(fld) {
	client.ajax('account/checkit', 'loginX=' + fld.val().uri(), function(rsp) {
		if (rsp.responseText) fld.addClass('lathos');
		else fld.removeClass('lathos');
	});
}

Account.working = function(prm) {
	$('#workingIcon').remove();
	if (prm === false) return;
	if (prm === undefined) prm = 'working/default.gif';

	return $('<img>').attr({
		id: 'workingIcon',
		src: '../ikona/' + prm,
	}).appendTo(Account.formaDOM);
}
